package com.sinosoft.vaccinetoai.common;

import com.sinosoft.vaccinetoai.exception.BusinessException;
import com.sinosoft.vaccinetoai.exception.ExceptionEnums;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * API结果类
 * @author xzh
 * @date 2022-10-24 11:09
 * @since 5.0.0
 */
@Getter
@Setter
@ToString
public class ApiResult {

    public ApiResult() {}

    public ApiResult(int code) {
        this.code = code;
    }

    public ApiResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ApiResult(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static ApiResult ofBusinessException(BusinessException exception) {
        return new ApiResult(exception.getCode(), exception.getMessage(), exception.getData());
    }

    public static ApiResult ofExceptionEnums(ExceptionEnums enums) {
        return new ApiResult(enums.getCode(), enums.getMessage(), null);
    }

    public static ApiResult ofExceptionEnums(ExceptionEnums enums, Object data) {
        return new ApiResult(enums.getCode(), enums.getMessage(), data);
    }

    public static ApiResult success(Object data) {
        return new ApiResult(ExceptionEnums.SUCCESS.getCode(), null, data);
    }

    //异常码
    private int code;

    //异常信息
    private String message;

    //数据
    private Object data;

}
